package com.icbc.open.chengdu.demo.account.openclose;

import com.csii.pp.icbc.util.*;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import com.icbc.api.request.SettlementAccountOpenRequestV1;
import com.icbc.api.response.SettlementAccountOpenResponseV1;
import com.icbc.open.chengdu.demo.BaseData;
import com.icbc.open.chengdu.demo.utils.SaveFileUtils;
import com.icbc.open.chengdu.demo.utils.GenerateKeyUtils;
import com.icbc.open.chengdu.demo.utils.SecretUtils;

/**
 * 开户申请
 *
 * @author xcz
 */
public class SettlementAccountOpenTest {


    public static void main(String[] args) throws Exception {
        String sm4Key = GenerateKeyUtils.generateSM4Key();  //生成SM4密钥
        DefaultIcbcClient client = new DefaultIcbcClient(BaseData.APP_ID, BaseData.MY_RSA_PRIVATE_KEY, BaseData.ICBC_RSA_PUBLIC_KEY);
        SettlementAccountOpenRequestV1 request = new SettlementAccountOpenRequestV1();
        SettlementAccountOpenRequestV1.SettlementAccountOpenRequestV1Biz bizContent = new SettlementAccountOpenRequestV1.SettlementAccountOpenRequestV1Biz();
        request.setServiceUrl(BaseData.ACCOUNT_OPEN_URL); //API地址
        bizContent.setCorpNo(BaseData.APP_ID);//合作方机构编号
        bizContent.setTrxAccDate(BaseData.TEST_TRX_ACC_DATE); //合作方交易日期
        bizContent.setTrxAccTime(BaseData.TEST_TRX_ACC_TIME);//合作方交易时间
        bizContent.setCorpDate(BaseData.TEST_CORP_DATE); //合作方工作日期
        String corpSerNo = Util.getNonceStr(16);
        SaveFileUtils.setCloseAccount(corpSerNo);//保存单号，短信验证/订单查询时使用
        bizContent.setCorpSerno(corpSerNo); //合作方交易单号
        bizContent.setOutServiceCode("openaccount"); //外部服务代码
        bizContent.setCorpMediumId(BaseData.TEST_CORP_MEDIUM_ID);//合作方介质号，不能重复，介质不同对应可以开辟多个II类账户
        bizContent.setBindMedium(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_ACCOUNT_NUMBER_1));//sm4加密的一类户卡号
        bizContent.setCertType(0); //证件类型
        bizContent.setCertNo(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_CERT_NO));  //SM4加密的证件号码  使用ECB会报324异常
        bizContent.setCustName(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_CUS_NAME));//SM4加密的客户姓名
        bizContent.setMobileNo(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_PHONE_NUMBER));//SM4加密的手机号码
        bizContent.setOccupation(20);//职业
        bizContent.setValidityPeriod("2037-05-17");//证件失效期
        bizContent.setSignDate("2017-05-17"); //证件签发日期
        bizContent.setSecretKey(SecretUtils.encryptSM2(sm4Key)); //使用SM2加密的SM4对称密钥
        bizContent.setBindMediumHash(MD5Util.string2MD5(BaseData.TEST_ACCOUNT_NUMBER_1)); //绑定的I类卡的HASH值
        bizContent.setNotifyAddr(BaseData.TEST_NOTIFY_ADDR);
        request.setBizContent(bizContent);
        SettlementAccountOpenResponseV1 response = client.execute(request);
        System.out.println(JSON.toJSONString(response));
        if (response.isSuccess() && response.getReturnCode() == 0) {
            SaveFileUtils.setSmsSendNo(response.getSmsSendNo());//保存短信验证码编号
        } else {
        }

    }


}
